import datetime
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


# 用户表
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    if_adm = db.Column(db.Boolean, default=False)
    username = db.Column(db.String(50), nullable=False, unique=True)
    phone = db.Column(db.String(11), nullable=False, unique=True)
    password = db.Column(db.String(200), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)


# 项目表
class Project(db.Model):
    __tablename__ = 'project'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  # 关联user表
    name = db.Column(db.String(32), nullable=False)  # 项目名称
    desc = db.Column(db.String(128), nullable=False)  # 项目描述
    key = db.Column(db.String(32), nullable=False)  # 项目key
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)


class FirmwareCondition:
    UPDATE_DISABLE = 0  # 禁止升级
    UPDATE_ALL = 1  # 升级全部
    UPDATE_PART = 2  # 部分升级


# 升级文件信息表
class Firmware(db.Model):
    __tablename__ = 'firmware'
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)  # 关联项目表
    name = db.Column(db.String(128), nullable=False)  # 固件名称
    version = db.Column(db.String(16), nullable=False)  # 固件版本
    url = db.Column(db.String(256), nullable=False)  # 固件云存储的url
    condition = db.Column(db.Integer, nullable=False)  # 启用条件 参考FirmwareCondition定义
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)


# 部分升级时指定的设备列表
class FirmwareDevices(db.Model):
    __tablename__ = 'firmwaredevices'
    id = db.Column(db.Integer, primary_key=True)
    firmware_id = db.Column(db.Integer, db.ForeignKey('firmware.id'), nullable=False)  # 关联升级文件信息表
    device_id = db.Column(db.Integer, db.ForeignKey('device.id'), nullable=False)  # 关联设备信息表
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)


# 设备信息表
class Device(db.Model):
    __tablename__ = 'device'
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('project.id'))  # 关联项目表 设备归属的项目
    uuid = db.Column(db.String(32), unique=True, index=True)  # 设备唯一id
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)


# 转移任务表
class AssignTask(db.Model):
    __tablename__ = 'assigntask'
    id = db.Column(db.Integer, primary_key=True)
    create_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  # 创建人 关联user表
    old_project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)  # 设备原来归属的项目id 关联项目表
    old_user_name = db.Column(db.String(32), nullable=False)  # 设备原来归属的项目的所属用户名称
    old_project_name = db.Column(db.String(64), nullable=False)  # 设备原来归属的项目名
    new_project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)  # 设备新的归属的项目id 关联项目表
    new_user_name = db.Column(db.String(32), nullable=False)  # 设备新的归属的项目的所属用户名称
    new_project_name = db.Column(db.String(64), nullable=False)  # 设备新的归属的项目名
    state = db.Column(db.String(32), nullable=False)  # 任务执行状态，进行中、成功、失败及失败原因
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)  # 开始时间
    finished_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)  # 结束时间


# 转移任务关联的设备列表
class AssignTaskDevice(db.Model):
    __tablename__ = 'assigntaskdevice'
    id = db.Column(db.Integer, primary_key=True)
    task_id = db.Column(db.Integer, db.ForeignKey('assigntask.id'), nullable=False)  # 关联AssignTask表
    device_uuid = db.Column(db.String(32), nullable=False)  # 设备uuid


# 升级日志表
class UpdateLog(db.Model):
    __tablename__ = 'updatelog'
    id = db.Column(db.Integer, primary_key=True)
    device_uuid = db.Column(db.String(32), nullable=False)  # 设备uuid
    device_user_name = db.Column(db.String(32))  # 设备请求当时归属的用户
    device_project_name = db.Column(db.String(32))  # 设备请求当时归属的项目名称
    request_time = db.Column(db.DateTime)  # 请求时间
    request_version = db.Column(db.String(128))  # 请求固件版本
    response_code = db.Column(db.Integer)  # 服务器响应的状态码
    response_version = db.Column(db.String(128))  # 服务器响应的固件版本
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
